package com.acme.bankapp.service.network;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * Class provides bank stress test functionality
 * @author alex
 */
public class BankStressTest extends BankNetwork {
    private static final int SLEEP_TIME = 20;

    /**
     * The main method of class
     * @param args the command-line parameters
     */
    public static void main(String[] args) {
        (new BankStressTest()).startStressTest();
    }

    /**
     * Start stress test
     */
    public void startStressTest() {
        try (Socket socket = new Socket(BankNetwork.SERVER_HOST, BankNetwork.SERVER_PORT);
             BufferedReader tempReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             BufferedWriter tempWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) {

            this.reader = tempReader;
            this.writer = tempWriter;
            System.out.println("==Connected to server==");

            Random random = new Random();
            while (true) {
                sendMessage(BankNetwork.COMMAND_ADD_CLIENT);
                sendMessage("accounttype=c;balance=3;overdraft=3;name=Client " + random.nextInt(Integer.MAX_VALUE) + ";gender=f;");
                recieveMessage();

                try {
                    Thread.sleep(SLEEP_TIME);
                } catch (InterruptedException e) {
                    System.out.println("Err: Thread interrupted!");
                }
            }
        } catch (IOException e) {
            System.out.println("Err: Network error!");
        }
    }
}
